function cmap=get_colormap(nc,nl,opts)

[temp_c , temp_l] = meshgrid(1:nc,1:nl);
color_ind = temp_c(:);
lightness_ind = temp_l(:);

if ischar(opts.map)

    %Brewer colormaps from http://colorbrewer2.org
    switch opts.map
        case 'matlab'
            cmap=lines();
            cmap_nc = 8;
            cmap_nl = 1;
        case 'brewer1'
            cmap=[228    26    28
                55   126   184
                77   175    74
                152    78   163
                255   127     0
                255   255    51
                166    86    40
                247   129   191
                153   153   153]/255;
            cmap_nc = 9;
            cmap_nl = 1;
        case 'brewer2'
            cmap=[102	194	165
                252	141	98
                141	160	203
                231	138	195
                166	216	84
                255	217	47
                229	196	148
                179	179	179]/255;
            cmap_nc = 8;
            cmap_nl = 1;
        case 'brewer3'
            
            cmap=[141	211	199
                255	255	179
                190	186	218
                251	128	114
                128	177	211
                253	180	98
                179	222	105
                252	205	229
                217	217	217
                188	128	189
                204	235	197
                255	237	111]/255;
            cmap_nc = 12;
            cmap_nl = 1;
        case 'brewer_pastel'
            cmap=[251	180	174
                179	205	227
                204	235	197
                222	203	228
                254	217	166
                255	255	204
                229	216	189
                253	218	236
                242	242	242]/255;
            cmap_nc = 9;
            cmap_nl = 1;
        case 'brewer_dark'
            cmap=[27	158	119
                217	95	2
                117	112	179
                231	41	138
                102	166	30
                230	171	2
                166	118	29
                102	102	102]/255;
            cmap_nc = 8;
            cmap_nl = 1;
        case 'brewer_paired' % 5 colors, 2 lightness levels for each
            cmap = [166,206,227
                31,120,180
                178,223,138
                51,160,44
                251,154,153
                227,26,28
                253,191,111
                255,127,0
                202,178,214
                106,61,154]/255;
            cmap_nc = 5;
            cmap_nl = 2;
        case 'd3_10'
            % 10 categories colormap from D3.js https://github.com/d3/d3-scale
            % Color values from matplotlib source code https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/_cm.py
            cmap = [0.12156862745098039, 0.4666666666666667,  0.7058823529411765
                1.0,                 0.4980392156862745,  0.0549019607843137
                0.17254901960784313, 0.6274509803921569,  0.1725490196078431
                0.8392156862745098,  0.15294117647058825, 0.1568627450980392
                0.5803921568627451,  0.403921568627451,   0.7411764705882353
                0.5490196078431373,  0.33725490196078434, 0.2941176470588235
                0.8901960784313725,  0.4666666666666667,  0.7607843137254902
                0.4980392156862745,  0.4980392156862745,  0.4980392156862745
                0.7372549019607844,  0.7411764705882353,  0.1333333333333333
                0.09019607843137255, 0.7450980392156863,  0.8117647058823529 ];
            cmap_nc = 10;
            cmap_nl = 1;
        case 'd3_20' % 10 colors, 2 lightness levels for each
            cmap = [0.12156862745098039, 0.4666666666666667,  0.7058823529411765
                0.6823529411764706,  0.7803921568627451,  0.9098039215686274
                1.0,                 0.4980392156862745,  0.0549019607843137
                1.0,                 0.7333333333333333,  0.4705882352941176
                0.17254901960784313, 0.6274509803921569,  0.1725490196078431
                0.596078431372549,   0.8745098039215686,  0.5411764705882353
                0.8392156862745098,  0.15294117647058825, 0.1568627450980392
                1.0,                 0.596078431372549,   0.5882352941176471
                0.5803921568627451,  0.403921568627451,   0.7411764705882353
                0.7725490196078432,  0.6901960784313725,  0.8352941176470589
                0.5490196078431373,  0.33725490196078434, 0.2941176470588235
                0.7686274509803922,  0.611764705882353,   0.5803921568627451
                0.8901960784313725,  0.4666666666666667,  0.7607843137254902
                0.9686274509803922,  0.7137254901960784,  0.8235294117647058
                0.4980392156862745,  0.4980392156862745,  0.4980392156862745
                0.7803921568627451,  0.7803921568627451,  0.7803921568627451
                0.7372549019607844,  0.7411764705882353,  0.1333333333333333
                0.8588235294117647,  0.8588235294117647,  0.5529411764705883
                0.09019607843137255, 0.7450980392156863,  0.8117647058823529
                0.6196078431372549,  0.8549019607843137,  0.8980392156862745];
            cmap_nc = 10;
            cmap_nl = 2;
        case 'd3_20b' % 5 colors, 4 lightness levels for each
            cmap = [0.2235294117647059,  0.23137254901960785, 0.4745098039215686
                0.3215686274509804,  0.32941176470588235, 0.6392156862745098
                0.4196078431372549,  0.43137254901960786, 0.8117647058823529
                0.611764705882353,   0.6196078431372549,  0.8705882352941177
                0.38823529411764707, 0.4745098039215686,  0.2235294117647059
                0.5490196078431373,  0.6352941176470588,  0.3215686274509804
                0.7098039215686275,  0.8117647058823529,  0.4196078431372549
                0.807843137254902,   0.8588235294117647,  0.611764705882353
                0.5490196078431373,  0.42745098039215684, 0.1921568627450980
                0.7411764705882353,  0.6196078431372549,  0.2235294117647059
                0.9058823529411765,  0.7294117647058823,  0.3215686274509804
                0.9058823529411765,  0.796078431372549,   0.5803921568627451
                0.5176470588235295,  0.23529411764705882, 0.2235294117647059
                0.6784313725490196,  0.28627450980392155, 0.2901960784313726
                0.8392156862745098,  0.3803921568627451,  0.4196078431372549
                0.9058823529411765,  0.5882352941176471,  0.611764705882353
                0.4823529411764706,  0.2549019607843137,  0.4509803921568627
                0.6470588235294118,  0.3176470588235294,  0.5803921568627451
                0.807843137254902,   0.42745098039215684, 0.7411764705882353
                0.8705882352941177,  0.6196078431372549,  0.8392156862745098];
            cmap_nc = 5;
            cmap_nl = 4;
        case 'd3_20c' % 5 colors, 4 lightness levels for each
            cmap = [0.19215686274509805, 0.5098039215686274,  0.7411764705882353
                0.4196078431372549,  0.6823529411764706,  0.8392156862745098
                0.6196078431372549,  0.792156862745098,   0.8823529411764706
                0.7764705882352941,  0.8588235294117647,  0.9372549019607843
                0.9019607843137255,  0.3333333333333333,  0.0509803921568627
                0.9921568627450981,  0.5529411764705883,  0.2352941176470588
                0.9921568627450981,  0.6823529411764706,  0.4196078431372549
                0.9921568627450981,  0.8156862745098039,  0.6352941176470588
                0.19215686274509805, 0.6392156862745098,  0.3294117647058823
                0.4549019607843137,  0.7686274509803922,  0.4627450980392157
                0.6313725490196078,  0.8509803921568627,  0.6078431372549019
                0.7803921568627451,  0.9137254901960784,  0.7529411764705882
                0.4588235294117647,  0.4196078431372549,  0.6941176470588235
                0.6196078431372549,  0.6039215686274509,  0.7843137254901961
                0.7372549019607844,  0.7411764705882353,  0.8627450980392157
                0.8549019607843137,  0.8549019607843137,  0.9215686274509803
                0.38823529411764707, 0.38823529411764707, 0.3882352941176470
                0.5882352941176471,  0.5882352941176471,  0.5882352941176471
                0.7411764705882353,  0.7411764705882353,  0.7411764705882353
                0.8509803921568627,  0.8509803921568627,  0.8509803921568627];
            cmap_nc = 5;
            cmap_nl = 4;
        case 'pm' %Paris metro
            cmap= [255 205 1
                0 108 184
                155 153 59
                109 197 224 %3bis
                187 75 156
                246 143 75
                119 198 150
                245 159 179
                197 164 205
                206 201 43
                224 176 59
                144 96 48
                0 139 90
                135 211 223 %Close to 3bis
                101 44 144
                169 15 50
                236 124 174
                149 191 50]/255;
            cmap_nc = 18;
            cmap_nl = 1;
        otherwise
            % Generate colormap using low-level function found on https://code.google.com/p/p-and-a/
            if nl==1
                %Was 65,75
                cmap=pa_LCH2RGB([repmat(linspace(opts.lightness,opts.lightness,nl)',nc+1,1) ...
                    repmat(linspace(opts.chroma,opts.chroma,nl)',nc+1,1)...
                    reshape(repmat(linspace(opts.hue_range(1),opts.hue_range(2),nc+1),nl,1),nl*(nc+1),1)],false);
            else
                cmap=pa_LCH2RGB([repmat(linspace(opts.lightness_range(1),opts.lightness_range(2),nl)',nc+1,1) ...
                    repmat(linspace(opts.chroma_range(1),opts.chroma_range(2),nl)',nc+1,1)...
                    reshape(repmat(linspace(opts.hue_range(1),opts.hue_range(2),nc+1),nl,1),nl*(nc+1),1)],false);
            end
            cmap_nc = nc + 1;
            cmap_nl = nl;
    end
else
    %For custom colormaps
    if isempty(opts.n_color) || isempty(opts.n_lightness)
        disp('Custom colormap without n_color and n_lightness specified, defaults to n_lightness=1')
        cmap_nc = size(opts.map,1);
        cmap_nl = 1;
    else
        cmap_nc = opts.n_color;
        cmap_nl = opts.n_lightness;
    end
    cmap=opts.map;
end

if nc > cmap_nc
    error('Too many color categories for this colormap');
end
if nl > cmap_nl
    error('Too many lightness categories for this colormap');
end

cmap_ind = sub2ind([cmap_nl cmap_nc],lightness_ind,color_ind);

cmap = cmap(cmap_ind , :);

end